package jmind.core.redis;

import com.google.common.collect.Maps;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * description: redis map操作
 * @author weibo.xie
 * @date:2022/8/23 上午11:32
 */
public class RedisMap implements Map<String,String> {
    private Redis redis ;
    private String prefixKey;

    public RedisMap(Redis redis,String prefixKey){
        this.redis=redis;
        this.prefixKey=prefixKey;
    }
    @Override
    public int size() {
        return redis.hlen(prefixKey).intValue();
    }

    @Override
    public boolean isEmpty() {
        return size()==0;
    }

    @Override
    public boolean containsKey(Object key) {
        return redis.hexists(prefixKey,key.toString());
    }

    @Override
    public boolean containsValue(Object value) {
        return false;
    }

    @Override
    public String get(Object key) {
        return redis.hget(prefixKey,key.toString());
    }

    @Override
    public String put(String key, String value) {
         redis.hset(prefixKey, key, value);
        return value;
    }

    @Override
    public String remove(Object key) {
         redis.hdel(prefixKey,key.toString());
         return null;
    }

    @Override
    public void putAll(Map<? extends String,? extends String> m) {
        redis.hmset(prefixKey,(Map<String,String>)m);
    }

    @Override
    public void clear() {
      redis.del(prefixKey);
    }

    @Override
    public Set<String> keySet() {
        return redis.hkeys(prefixKey);
    }

    @Override
    public Collection<String> values() {
        return redis.hvals(prefixKey);
    }

    @Override
    public Set<Entry<String, String>> entrySet() {
        throw new UnsupportedOperationException("redisMap not support entrySet use getAll()");
    }

    public Map<String,String>  getAll(){
        return redis.hgetAll(prefixKey);
    }

    public static void main(String[] args) {
        HashMap<String,String> map= Maps.newHashMap();
        System.err.println(map.put("a","A"));
        System.err.println(map.put("a","b"));

        map.entrySet();
    }
}
